package data_structur.LinkList;

/**
 * 双向链表：既有next，也有pre
 * 双向链表与单链表相比：双向链表可以向前或向后查找，单链表查找只能是一个方向；
 *                      可以实现自我删除，不用依靠辅助节点。
 */
public class DoubleLinkListDemo {

    public static void main(String[] args) {
        DoubleLinkList doubleLinkList = new DoubleLinkList();
        doubleLinkList.list();
        doubleLinkList.update(new DoubleHeroNode(4, "x", "X"));
        doubleLinkList.delete(4);
        doubleLinkList.add(new DoubleHeroNode(1, "a", "A"));
        doubleLinkList.add(new DoubleHeroNode(2, "b", "B"));
        doubleLinkList.add(new DoubleHeroNode(3, "c", "C"));
        doubleLinkList.add(new DoubleHeroNode(4, "d", "D"));
        doubleLinkList.add(new DoubleHeroNode(5, "e", "E"));
        doubleLinkList.list();
        doubleLinkList.update(new DoubleHeroNode(4, "x", "X"));
        doubleLinkList.list();
        doubleLinkList.delete(4);
        doubleLinkList.list();
    }

}

class DoubleLinkList {

    //双链设头
    private DoubleHeroNode head = new DoubleHeroNode(0, "", "");

    // 双链找头
    public DoubleHeroNode getHead(){
        return this.head;
    }

    // 双链遍历显示
    public void list(){
        System.out.println("DoubleLinkList:");
        if (head.next == null){
            System.out.println("DoubleLinkList is Empty, Fail to Show List.");
        } else {
            DoubleHeroNode temp = head.next;
            while (temp != null){
                System.out.println(temp);
                temp = temp.next;
            }
        }
    }

    // 双链增节点至末尾
    public void add(DoubleHeroNode doubleHeroNode){
        DoubleHeroNode temp = this.head;
        while (temp.next != null) {
            temp = temp.next;
        }
        temp.next = doubleHeroNode;
        doubleHeroNode.pre = temp;
    }

    // 双链根据num更新节点
    public void update(DoubleHeroNode doubleHeroNode){
        if (head.next == null){
            System.out.println("DoubleLinkList is Empty, Fail to Update.");
            return;
        }
        DoubleHeroNode temp = head;
        while (true){
            if (temp.next == null){
                System.out.println("No Such DoubleHeroNode, Fail to Update.");
                return;
            }
            if (temp.num == doubleHeroNode.num){
                doubleHeroNode.pre = temp.pre;
                doubleHeroNode.next = temp.next;
                temp.pre.next = doubleHeroNode;
                return;
            } else {
                temp = temp.next;
            }
        }
    }

    // 双链只根据num删除节点
    public void delete(int num){
        DoubleHeroNode temp = head;
        while (true){
            if (temp.next == null){
                System.out.println("No Such DoubleHeroNode, Fail to Delete.");
                return;
            }
            if (temp.num == num){
                temp.pre.next = temp.next;
                temp.next.pre = temp.pre;
                return;
            } else {
                temp = temp.next;
            }
        }
    }
}

class DoubleHeroNode {
    public int num;
    public String name;
    public String nickName;
    public DoubleHeroNode pre;
    public DoubleHeroNode next;
    //构造器
    public DoubleHeroNode(int num, String name, String nickName){
        this.num = num;
        this.name = name;
        this.nickName = nickName;
    }

    /**
     * 重写toString方法
     * @return
     */
    @Override
    public String toString() {
        return "{DoubleHeroNode: [id: " + this.num + "], [name: " + this.name + "], [nickname: " + this.nickName + "]}";
    }
}
